package com.qxgltj.qxgl.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.qxgltj.qxgl.dao.IUsersDao;
import com.qxgltj.qxgl.dto.UsersDTO;
import com.qxgltj.qxgl.model.Roles;
import com.qxgltj.qxgl.model.Users;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements IUsersService {
    @Resource
    private IUsersDao usersDao;
    @Override
    public Page<Users> findPage(UsersDTO dto) {
        Sort.Order order = new Sort.Order(Sort.Direction.ASC, "Id");
        PageRequest request = PageRequest.of((dto.getPage() - 1) * dto.getLimit(), dto.getLimit(), Sort.by(order));
        return usersDao.findAll(getSpec(dto), request);
    }

    private Specification<Users> getSpec(final UsersDTO dto) {
        return new Specification<Users>() {
            /**
             *
             */
            private static final long serialVersionUID = 1L;

            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query,
                    CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.conjunction();
                List<Expression<Boolean>> exList = p.getExpressions();// 动态SQL表达式集合
                if (dto.getLoginName() != null && !"".equals(dto.getLoginName())) {
                    exList.add(criteriaBuilder.like(root.get("loginName"), "%" + dto.getLoginName() + "%"));
                    System.out.println("条件判断不为空");
                }
                if (dto.getPassword() != null && !"".equals(dto.getPassword())) {
                    exList.add(criteriaBuilder.equal(root.<String>get("password"), dto.getPassword()));
                }
                if (dto.getIslockout() != null && !"".equals(dto.getIslockout())) {
                    exList.add(criteriaBuilder.equal(root.<String>get("islockout"), dto.getIslockout()));
                }
                if (dto.getLastlogintime() != null) {
                    exList.add(
                            criteriaBuilder.lessThanOrEqualTo(root.<Date>get("lastlogintime"), dto.getLastlogintime()));
                }
                if (dto.getCreatetime() != null) {
                    exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createtime"), dto.getCreatetime()));
                }
                return p;
            }
        };
    }

    @Override
    public Users saveUsers(Users u) {
        return usersDao.save(u);
    }

    @Override
    public void del(String id) {
        usersDao.deleteById(id);
    }

    @Override
    public void setRoles(String user_id, List<String> roleslist) {
        Users users=usersDao.getOne(user_id);
        for(String role_id:roleslist){
                       Roles roles=new Roles();
                       roles.setId(role_id);
                       users.getRolesList().add(roles);

        }
        usersDao.save(users);

    }
}